//
//  HWGameCommentInteractController.swift
//  HWanGame
//
//  Created by 王昌健 on 2022/10/24.
//  Copyright © 2022 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView
import IQKeyboardManagerSwift
class HWGameCommentInteractController: HWBaseViewController {
    ///标签
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    var keyboardView:HWCommentKeyboardView!
    var httpManager = HWGameCommunityHttpManager()
    var replyUserModel = HWGameCommunityReplyMessageModel()
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        IQKeyboardManager.shared.enable = false
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
//        IQKeyboardManager.shared.enable = true
    }
}

extension HWGameCommentInteractController {
    private func initUI() -> Void {
        
        navigationView.titleLabel.text = "帖子和互动"
        navigationView.backButton.setImage(UIImage(named: "navBackButton"), for: .normal)
        
        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["互动消息","我的帖子"]
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#333333")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#FA8C08")
        segmentedDataSource.titleNormalFont = .systemFont(ofSize: 14*widthScale)
        segmentedDataSource.titleSelectedFont = .boldSystemFont(ofSize: 14*widthScale)
        segmentedDataSource.isItemSpacingAverageEnabled = false
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorLineView()
        indicator.indicatorColor = .colorWithHexString(hexString: "#FA8C08")
        indicator.indicatorCornerRadius = 0
        indicator.verticalOffset = 0
        indicator.indicatorHeight = 4*widthScale
        segmentedView.indicators = [indicator]
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        view.addSubview(self.segmentedView)
        
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        keyboardView = HWCommentKeyboardView()
        view.addSubview(keyboardView)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChangeFrameNotify(_:)), name:UIResponder.keyboardWillChangeFrameNotification, object: nil)
    }
    
    private func initFrame() -> Void {
        segmentedView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(navigationView.snp.bottom)
            make.height.equalTo(30*widthScale)
        }
        listContainerView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(segmentedView.snp.bottom).offset(10*widthScale)
        }
        
        keyboardView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.bottom.equalTo(54)
        }
    }
}

extension HWGameCommentInteractController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let message = HWGameCommentInteractMessageController()
        let posts = HWGameCommentInteractPostsController()
        if index == 0 {
            message.replyClick = {
                mod in
                self.replyUserModel = mod
                self.keyboardView.textField.becomeFirstResponder()
            }
            return message
        } else {
            return posts
        }
        
    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        
    }
}

extension HWGameCommentInteractController {
    @objc func keyboardWillChangeFrameNotify(_ notify:NSNotification) -> Void {
        let duration = notify.userInfo![UIResponder.keyboardAnimationDurationUserInfoKey] as! Double
        let keyboardFrame = notify.userInfo![UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
        let transformY = keyboardFrame.origin.y - self.view.frame.size.height;
        UIView.animate(withDuration: duration) {
            
            if transformY == 0.0 {
                
                self.keyboardView.transform = CGAffineTransform.init(translationX: 0, y: transformY)
                self.keyboardView.textField.placeholder = "请输入评论内容..."
                self.keyboardView.textField.text = ""
            }else{
                if screenHeight < 812 {
                    self.keyboardView.transform = CGAffineTransform.init(translationX: 0, y: transformY+54)
                    
                }else {
                    self.keyboardView.transform = CGAffineTransform.init(translationX: 0, y: transformY-54)
                }
            }
            
        }
    }
}

extension HWGameCommentInteractController {
    func addAction() -> Void {
        self.keyboardView.sendText = {
            text in
            self.httpManager.getGameCommunityCommentAdd(pid: self.replyUserModel.pid, puid: self.replyUserModel.pid, type: "1", content: text) { nessage in
                self.keyboardView.textField.text = ""
                self.keyboardView.textField.endEditing(true)
            } failure: { errorString in
                
            }
        }
    }
}
